import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/pages/index/Index'
import Over from '@/pages/index/Over'
import Full from '@/pages/index/Full'
import Result from '@/pages/index/Result'
import utils from '@/utils'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index
    }, {
      path: '/over',
      name: 'Over',
      component: Over
    }, {
      path: '/result',
      name: 'Result',
      component: Result
    }, {
      path: '/full',
      name: 'Full',
      component: Full
    }
  ]
})


router.beforeEach((to, from, next) => {
  const discern = localStorage.getItem('camellia-sample-discern') || to.query.discern;
  let channel = utils.getChannelInfo().channel
  utils.isOver(discern).then(res => {
    let isOver = res
    console.log(isOver)
    if (channel) {
      if (to.name === 'Over') {
        next()
      }
      else if (isOver) {
        next('/over')
      }
      else {
        if (discern && !isOver && to.name === 'Index') {
          next({ path: '/result', query: { discern } })
        } else {
          next()
        }
      }
    } else {
      next()
    }
  })
})

export default router
